package com.yndsoft.jc.ui.memberpage.myorder;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.OrderDet;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/7.
 */

public class OrderDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.order_detail_list)
    ListView order_detail_list;
    List<OrderDet> mData = new ArrayList<>();
    OrderDetailAdapter adapter;
    LayoutInflater inflater;
    View headView;
    View footView;
    TextView order_detail_evaluate;//评价
    private int productID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail_ly);

//        setDatas();
        setViews();
        getDatas();
        setListener();
    }

//    private void setDatas() {
//        for (int i = 0; i < 6; i++) {
//            mData.add("婴幼儿奶粉" + i);
//        }
//    }

    private void getDatas() {
        mHttpUtil.getOrderDetail(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                Type type = new TypeToken<MyOrder>() {
                }.getType();
                Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                MyOrder order = (MyOrder) fromJson;
                if (order.getState() == 1) {
                    icon_order_state.setImageResource(R.mipmap.icon_will_pay);
                    AppUtils.setText(tv_order_state, "待付款");
                } else if (order.getState() == 2) {
                    icon_order_state.setImageResource(R.mipmap.icon_paied);
                    AppUtils.setText(tv_order_state, "已付款");
                } else if (order.getState() == 8) {
                    icon_order_state.setImageResource(R.mipmap.icon_pay_complete);
                    AppUtils.setText(tv_order_state, "检测完成");
                } else {
                    icon_order_state.setImageResource(R.mipmap.icon_waiting_result);
                    AppUtils.setText(tv_order_state, "等待检测结果");
                }
                if (order.getMemberaddress() != null) {
                    send_people.setText("送检人：" + order.getMemberaddress().getName());
                    send_people_cell.setText(order.getMemberaddress().getPhone());
                    send_people_adrs.setText(order.getMemberaddress().getAddress());
                }
                adapter.setDatas(order.getDets());
                order_detail_totalPrice.setText("￥" + order.getTotalprice());
                actual_pay.setText("￥" + order.getPayprice());
                AppUtils.setText(codeno, order.getCodeno());
                AppUtils.setText(order_ID, order.getId() + "");
                AppUtils.setText(order_create_time, order.getCreatetime());
            }
        }, "order", mSession.getUserInfo().getId(), productID);
    }

    TextView send_people, send_people_cell, send_people_adrs, tv_order_state;
    ImageView icon_order_state;//订单状态图标
    TextView order_detail_totalPrice;//商品总额
    TextView actual_pay;//实付款
    TextView codeno;//推荐码
    TextView order_ID;//订单编号
    TextView order_create_time;//下单时间

    private void setViews() {
        base_title.setText(getResources().getString(R.string.order_detail));
        productID = getIntent().getIntExtra("OrderID", 100001);

        inflater = LayoutInflater.from(mContext);
        headView = inflater.inflate(R.layout.header_order_detail, null);
        icon_order_state = headView.findViewById(R.id.icon_order_state);
        tv_order_state = headView.findViewById(R.id.tv_order_state);
        send_people = headView.findViewById(R.id.send_people);
        send_people_cell = headView.findViewById(R.id.send_people_cell);
        send_people_adrs = headView.findViewById(R.id.send_people_adrs);

        footView = inflater.inflate(R.layout.foot_order_detail, null);
        order_detail_evaluate = footView.findViewById(R.id.order_detail_evaluate);
        order_detail_totalPrice = footView.findViewById(R.id.order_detail_totalPrice);
        actual_pay = footView.findViewById(R.id.actual_pay);
        codeno = footView.findViewById(R.id.codeno);
        order_ID = footView.findViewById(R.id.order_ID);
        order_create_time = footView.findViewById(R.id.order_create_time);

        headView.findViewById(R.id.icon_go).setVisibility(View.GONE);
        adapter = new OrderDetailAdapter(mContext);
        order_detail_list.addHeaderView(headView);
        order_detail_list.addFooterView(footView);
        order_detail_list.setAdapter(adapter);
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        order_detail_evaluate.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.order_detail_evaluate:
                intent = new Intent(this, EvaluateActivity.class);
                intent.putExtra(ConstantValues.EVALUATE_INTENT, productID);
                startActivity(intent);
                break;
        }
    }
}
